#include <iostream>
#include <unordered_map>
#include <vector>
#include <queue>
using namespace std;

class Node
{
public:
    int weight;
    pair<int, int>link;
    friend bool operator>(const Node &a, const Node &b) {
        return a.weight > b.weight;
    }
    friend bool operator<(const Node &a, const Node &b) {
        return a.weight < b.weight;
    }
};

unordered_map<int, int> f;
priority_queue<Node, vector<Node>, greater<Node>>q;

int find(int x) {
    if (!f.count(x))
    {
        f[x] = x;
    }
    return f[x] == x ? x : f[x] = find(f[x]);
}

// true->x,y is connected, false->not connected
bool unionSet(int x, int y) {
    int fx = find(x), fy = find(y);
    if (fx == fy)
    {
        return true;
    }
    f[fy] = fx;
    return false;
}


int main() {
    int N, E;
    cin >> N >> E;
    for (int i = 0;i < E;i++)
    {
        int a, b, c;
        cin >> a >> b >> c;
        Node node;
        node.link = pair<int, int>(a, b);
        node.weight = c;
        q.push(node);
    }

    int sum = 0;

    while (!q.empty())
    {
        Node node = q.top();
        q.pop();
        if (unionSet(node.link.first, node.link.second) == true)
        {
            continue;
        }
        sum += node.weight;
    }
    cout << sum << endl;
    return 0;
}